package com.example.niuke;

import com.alibaba.fastjson.JSON;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadLocalTest {
    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(20));
    int a = 1;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            threadPoolExecutor.execute(() -> {
                threadLocal.set(finalI);
                System.out.println("第" + finalI + "个" + threadLocal.get());
                System.out.println(finalI + "" + Thread.currentThread());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("第" + finalI + "个" + threadLocal.get());

            });
        }
        for (int i = 0; i < 3; i++) {
            int finalI = i;
            threadPoolExecutor.execute(() -> {
                System.out.println(finalI + "第2轮" + threadLocal.get());
                System.out.println(finalI + "第2轮" + Thread.currentThread());
            });
        }
        System.out.println(JSON.toJSONString(threadLocal));
        System.out.println("我是最后输出的" + threadLocal.get());
    }

    void aaa(double a) {
        StringBuilder stringBuilder = new StringBuilder(String.valueOf(a));

        System.out.println(stringBuilder);
        System.out.println(Thread.currentThread().getName());
    }

    static class ThreadLocalTest1 {
        private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100, 100, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(20));

        public static void main(String[] args) {
            ThreadLocalTest threadLocalTest = new ThreadLocalTest();
            for (int i = 0; i < 10; i++) {
                int finalI = i;
                threadPoolExecutor.execute(() -> {
                    threadLocalTest.aaa(finalI);
                });
            }

        }
    }

}
